package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.ErciBean;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.YanShouJiChaBean;
import com.wnhz.work.yz.bean.Yasd;
import com.wnhz.work.yz.bean.YscjikhasofiojsBean;
import com.wnhz.work.yz.bean.YscpdBean;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YanShouCPDe444Activity extends BaseActivity implements View.OnClickListener {
    private RecyclerView recycler;
    //  private LinearLayout is_fangjian;
    private YscpdBean bean1 = new YscpdBean();
    private EditText room_num;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_cpde);
        EventBus.getDefault().register(this);
        //is_fangjian = (LinearLayout) findViewById(R.id.is_fangjian);
        room_num = (EditText) findViewById(R.id.room_num);
        if (!getIntent().getStringExtra("house").contains("未知")) {
            room_num.setText(getIntent().getStringExtra("house"));
            //room_num.setEnabled(false);
        }
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("查看详情");
        submit.setOnClickListener(this);
        submit.setVisibility(View.VISIBLE);

        //  is_fangjian.setVisibility(View.GONE);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        bean1 = (YscpdBean) getIntent().getSerializableExtra("bean");
        title.setText("" + getIntent().getStringExtra("name"));
        TextView company = (TextView) findViewById(R.id.company);
        company.setText("" + getIntent().getStringExtra("company"));
        TextView score = (TextView) findViewById(R.id.score);
        score.setText("得分：" + getIntent().getStringExtra("score") + "分");

        //
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycler.setAdapter(new BaseRVAdapter(this, bean1.getInfo()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_cpd;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.title, bean1.getInfo().get(position).getLabel());
                holder.setTextView(R.id.num, "共" + bean1.getInfo().get(position).getSum() + "件");

                RecyclerView recycler = holder.getView(R.id.recycler);
                recycler.setLayoutManager(new GridLayoutManager(YanShouCPDe444Activity.this, 2, LinearLayoutManager.VERTICAL, false));
                final List<YscpdBean.InfoEntity.ListEntity> list = bean1.getInfo().get(position).getList();
                recycler.setAdapter(new BaseRVAdapter(YanShouCPDe444Activity.this, list) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_yscpd;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, final int position) {
                        holder.setTextView(R.id.score, list.get(position).getScore() + "分");
                        holder.setTextView(R.id.name, list.get(position).getName());
                        x.image().bind(holder.getImageView(R.id.img), list.get(position).getImg());
                        TextView status = holder.getTextView(R.id.status);
                        status.setVisibility(View.GONE);
                        if ("0".equals(list.get(position).getStatus())) {
                            status.setText("未检");
                            status.setTextColor(getResources().getColor(R.color.text));
                            holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    //    gotoJian(0, list.get(position).getPid(), list.get(position).getType());
                                }
                            });
                        } else if ("1".equals(list.get(position).getStatus())) {
                            if ("1".equals(list.get(position).getIs_punish())) {
                                status.setText("有处罚");
                            } else {
                                status.setText("不合格");
                            }
                            status.setTextColor(getResources().getColor(R.color.red));
                            holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    //   gotoJian(1, list.get(position).getPid(), list.get(position).getType());
                                }
                            });
                        } else if ("2".equals(list.get(position).getStatus())) {
                            status.setText("已通过");
                            status.setTextColor(getResources().getColor(R.color.main_color));
                            holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    gotoJian(2, list.get(position).getPid(), list.get(position).getType());
                                }
                            });
                        }


                    }

                });

            }

        });
    }

    private void gotoJian(final int i, final String s, final String pid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("pid", s);
        map.put("type", pid);
        showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e(i + "----" + key, "" + value + "\n");
            }
        }
        XUtil.Post(Url.DECORATE_CHECKGOODSDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("--商品验收的页面--", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----1" + i, "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        if (i == 0) {
                            YscjikhasofiojsBean bean = gson.fromJson(result, YscjikhasofiojsBean.class);
                            startActivity(new Intent(YanShouCPDe444Activity.this, YanShouLingCiActivity.class)
                                    .putExtra("pid", s).putExtra("type", pid)
                                    .putExtra("bean", bean));
                        } else if (i == 1) {
                            ErciBean bean = gson.fromJson(result, ErciBean.class);
                            startActivity(new Intent(YanShouCPDe444Activity.this, YanShouErCiActivity.class).putExtra("pid", s).putExtra("type", pid)
                                    .putExtra("bean", bean));
                        } else if (i == 2) {
                            Yasd yasd = gson.fromJson(result, Yasd.class);
                            startActivity(new Intent(YanShouCPDe444Activity.this, YanShouYiCiActivity.class).putExtra("bean", yasd));
                        }

                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                Intent intent = getIntent();
                intent.putExtra("room_num", room_num.getText().toString());
                intent.putExtra("position", getIntent().getStringExtra("position"));
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.submit:
                getDetails();
                break;
        }
    }

    private void getDetails() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", getIntent().getStringExtra("orderid"));
        showDialog();
        Set keys = map.keySet();

        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e(99 + "----" + key, "" + value + "\n");
            }
        }

        XUtil.Post(Url.DECORATE_ANZHUANGDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtil.e("----安装详情---", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouJiChaBean bean = gson.fromJson(result, YanShouJiChaBean.class);
                        startActivity(new Intent(YanShouCPDe444Activity.this, JiChaDetails2Activity.class)
                                .putExtra("type", getIntent().getStringExtra("type"))
                                .putExtra("bean", bean).putExtra("orderid", getIntent().getStringExtra("orderid")));

                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = getIntent();
            intent.putExtra("room_num", room_num.getText().toString());
            intent.putExtra("position", getIntent().getStringExtra("position"));
            setResult(RESULT_OK, intent);
            finish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
